package com.slz3.business.module.configManagement.conZone.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conZone.domain.ConZone;
import com.slz3.business.module.configManagement.conZone.domain.ConZoneFactory;
import com.slz3.business.module.configManagement.conZone.steward.ConZoneSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConZoneServiceImpl implements ConZoneService {

    @Override
    public ConZone save(ConZone conZone) {
        return Repositorys.conZone.save(ConZoneFactory.initBaseFields(conZone));
    }

    @Override
    public Page<ConZone> page(MultiValueMap<String, String> params) {
        Page<ConZone> conZonePage = Repositorys.conZone.findAll(
                ConZoneSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conZonePage;
    }

    @Override
    public ConZone modify(ConZone conZoneNew) {
        return Repositorys.conZone.saveAndFlush(ConZoneFactory.initBaseFields(conZoneNew));
    }

    @Override
    public Optional<ConZone> single(UUID uuid) {
        return Repositorys.conZone.findById(uuid);
    }

    @Override
    public List<ConZone> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conZone.findAllById(uuids);
    }

    @Override
    public List<ConZone> delete(List<ConZone> conZoneList) {
        conZoneList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConZoneFactory.initBaseFields(item);
        });
        return Services.conZoneTransactional.batchModify(conZoneList);
    }

}
